﻿using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using EasyXaf.ObjectSpaces.OData;
using EasyXaf.Sample.Module.BusinessObjects;
using Microsoft.OData.Client;

namespace EasyXaf.Sample.Module.Controllers;

public class CompanyListViewController : ObjectViewController<ListView, Company>
{
    public SimpleAction DepartmentCountAction { get; }

    public SimpleAction UpdateCompanyNameAction { get; }

    public SimpleAction EntityAssociationTestAction { get; }

    public CompanyListViewController()
    {
        DepartmentCountAction = new SimpleAction(this, "DepartmentCountAction", PredefinedCategory.Edit)
        {
            Caption = "部门总个数",
        };

        UpdateCompanyNameAction = new SimpleAction(this, "UpdateCompanyNameAction", PredefinedCategory.Edit)
        {
            Caption = "更新公司名称"
        };

        EntityAssociationTestAction = new SimpleAction(this, "EntityAssociationTestAction", PredefinedCategory.Edit)
        {
            Caption = "实体关系测试"
        };
    }

    protected override void OnActivated()
    {
        base.OnActivated();

        DepartmentCountAction.Execute += DepartmentCountAction_Execute;
        UpdateCompanyNameAction.Execute += UpdateCompanyNameAction_Execute;
        EntityAssociationTestAction.Execute += EntityAssociationTestAction_Execute;
    }

    protected override void OnDeactivated()
    {
        DepartmentCountAction.Execute -= DepartmentCountAction_Execute;
        UpdateCompanyNameAction.Execute -= UpdateCompanyNameAction_Execute;
        EntityAssociationTestAction.Execute -= EntityAssociationTestAction_Execute;

        base.OnDeactivated();
    }

    private void DepartmentCountAction_Execute(object sender, SimpleActionExecuteEventArgs e)
    {
        var objectSpace = (IODataObjectSpace)ObjectSpace;
        var odataContext = objectSpace.ODataContext;
        var requestUri = new Uri(objectSpace.ServiceRoot + $"/{typeof(Company).GetEntitySetName()}/GetDepartmentCount()");
        var count = odataContext.Execute<int>(requestUri, "GET").FirstOrDefault();
        Application.ShowViewStrategy.ShowMessage(count + string.Empty);
    }

    private void UpdateCompanyNameAction_Execute(object sender, SimpleActionExecuteEventArgs e)
    {
        var objectSpace = (IODataObjectSpace)ObjectSpace;
        var odataContext = objectSpace.ODataContext;
        var requestUri = new Uri(objectSpace.ServiceRoot + $"/{typeof(Company).GetEntitySetName()}/UpdateCompanyName");
        odataContext.Execute(requestUri, "POST", new BodyOperationParameter("prefix", "A_"));
        objectSpace.Refresh();
    }

    private void EntityAssociationTestAction_Execute(object sender, SimpleActionExecuteEventArgs e)
    {
        if (View.SelectedObjects.Count > 1)
        {
            var companyA = View.SelectedObjects[0] as Company;
            var companyB = View.SelectedObjects[1] as Company;

            var department = companyA.Departments.First();
            department.Company = companyB;
            _ = companyB.Departments.First(d => d == department);

            department = companyB.Departments.First();
            department.Company = companyA;
            _ = companyA.Departments.First(d => d == department);
        }
    }
}
